<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>低值资产管理系统</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta content="width=device-width, initial-scale=1.0" name="viewport" />
	<meta content="" name="description" />
	<meta content="" name="author" />
	<meta name="MobileOptimized" content="320">
	<!-- BEGIN GLOBAL MANDATORY STYLES -->          
	<link href="${ctx}/assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css"/>
	<!-- END GLOBAL MANDATORY STYLES -->
	<!-- BEGIN PAGE LEVEL STYLES -->
	<link rel="stylesheet" type="text/css" href="${ctx}/assets/plugins/fuelux/css/tree-metronic.css" />
	<!-- END PAGE LEVEL STYLES -->
	<!-- BEGIN THEME STYLES --> 
	<link href="${ctx}/assets/css/style-metronic.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/css/style.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/css/style-responsive.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/css/plugins.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/css/themes/light.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx}/assets/css/custom.css" rel="stylesheet" type="text/css"/>
	<link href="${ctx }/css/custom.css" rel="stylesheet" type="text/css"/>
	<!-- END THEME STYLES -->	
	<link rel="stylesheet" href="${ctx }/zTree_v3/css/demo.css" type="text/css">
	<link rel="stylesheet" href="${ctx }/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<link rel="shortcut icon" href="favicon.ico" />
	<style type="text/css">
		.page-content{
			min-height:900px;
		}
	</style>
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body class="page-header-fixed">
	<!-- BEGIN HEADER -->
	<%@include file="../include/header.jsp" %>
	<!-- END HEADER -->	
	<div class="clearfix"></div>
	<!-- BEGIN CONTAINER -->   
	<div class="page-container">
		<!-- BEGIN SIDEBAR -->
		<%@include file="../include/sidebar.jsp" %>
		<!-- END SIDEBAR -->
		<!-- BEGIN PAGE -->
		<div class="page-content">
			<form id="userForm">
				<!-- BEGIN PAGE HEADER-->
				<div class="row">
					<div class="col-md-12">
						<!-- BEGIN PAGE TITLE & BREADCRUMB-->
						<h3 class="page-title">
							部门/用户设置
						</h3>
						<!-- END PAGE TITLE & BREADCRUMB-->
					</div>
				</div>
				<hr>
				<!-- END PAGE HEADER-->
				<!-- BEGIN PAGE CONTENT-->
				<div class="row">
					<div class="col-md-4">
						<div class="portlet purple box">
							<div class="portlet-title">
								<div class="caption"><i class="fa fa-cogs"></i>部门设置</div>
							</div>
							<div class="portlet-body" style="padding:0 0 0 0;">
								<!-- <div class="zTreeDemoBackground"> -->
								<ul id="ztree" class="ztree"></ul>
								<!-- </div> -->
							</div>
						</div>
					</div>
					<div class="col-md-8">
						<div class="portlet box blue">
							<div class="portlet-title">
								<div class="caption"><i class="fa fa-cogs"></i>部门设置</div>
							</div>
							<div class="portlet-body" id="depDetail"></div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-md-12">
						<div class="portlet box blue">
							<div class="portlet-title">
								<div class="caption"><i class="fa fa-cogs"></i>人员设置</div>
							</div>
							<div class="portlet-body">
								<div>
									<button class="btn blue" type="button" id="addUser">添加用户</button>
									<button class="btn blue" type="button" id="toChangeUserDep">更改部门</button>
								</div>
								<hr>
								<div class="portlet-body">
									<table class="table table-striped table-bordered table-hover" id="sample_1">
										<thead>
											<tr height="20px;">
												<th><input type="checkbox" id="userIdAll"></th>
												<th>登录名</th>
												<th>部门</th>
												<th>用户姓名</th>
												<th>邮箱</th>
												<th>角色</th>
												<th>操作</th>
											</tr>
										</thead>
										<tbody id="user"></tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
				</div>
			</form>
			<!-- END PAGE CONTENT-->
		</div>
		<!-- END PAGE -->    
	</div>
	<!-- END CONTAINER -->
	<!-- BEGIN FOOTER -->
	<%@include file="../include/footer.jsp" %>
	<!-- END FOOTER -->
	<script src="${ctx}/assets/plugins/jquery-1.10.2.min.js" type="text/javascript"></script>
	<script src="${ctx}/assets/plugins/jquery-migrate-1.2.1.min.js" type="text/javascript"></script>    
	<script src="${ctx}/assets/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
	<script src="${ctx}/assets/plugins/bootstrap-hover-dropdown/twitter-bootstrap-hover-dropdown.min.js" type="text/javascript" ></script>
	<script src="${ctx}/assets/plugins/jquery-slimscroll/jquery.slimscroll.min.js" type="text/javascript"></script>
	<script src="${ctx}/assets/plugins/jquery.blockui.min.js" type="text/javascript"></script>  
	<script src="${ctx}/assets/plugins/jquery.cookie.min.js" type="text/javascript"></script>
	<script src="${ctx}/assets/plugins/uniform/jquery.uniform.min.js" type="text/javascript" ></script>
	<!-- END CORE PLUGINS -->
	<!-- BEGIN PAGE LEVEL SCRIPTS -->
	<script src="${ctx}/assets/plugins/fuelux/js/tree.min.js"></script>  
	<!-- END PAGE LEVEL SCRIPTS -->
	<script src="${ctx}/assets/scripts/app.js?v=20200714"></script>
	<script type="text/javascript" src="${ctx }/zTree_v3/js/jquery.ztree.core-3.5.js"></script>
	<script>
	
		var selectID="";
	
		function zTreeOnClick(event, treeId, treeNode) {
		    //alert(treeNode.tId + ", " + treeNode.name);		    
		    selectID=treeNode.id;
		    if(selectID=="")
				$('#addUser').hide();
		    else
		    	$('#addUser').show();


            getDepUsers(selectID);
		    $.ajax({
		    	url:"${ctx}/system/getDepDetail.do?id="+selectID,
		    	type:"POST",
		    	success:function(data){
		    		$('#depDetail').html(data);
		    	}
		    });
		    
		}

		function getDepUsers(depId) {
            $.ajax({
                url:"${ctx}/system/getDepUsers.do?id="+depId,
                type:"POST",
                success:function(data){
                    $('#user').html(data);
                }
            });
        }
	
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: zTreeOnClick
			}
		};

		var zNodes =${result};
	
		jQuery(document).ready(function() {       
		   // initiate layout and plugins
		   App.init();
		   $('#J').addClass('active');
		   $('#J2').addClass('active');
		   $.fn.zTree.init($("#ztree"), setting, zNodes);
		   if(selectID=="") {
               $('#addUser').hide();
           } else {
               $('#addUser').show();
		   }

			$('#addUser').bind('click',function(){
				window.open("${ctx}/system/addUser.do?depId="+selectID,"blank");
			});

			$('#userIdAll').click(function(){
				if($('#userIdAll').attr("checked")){
					$("input[name='userId']:checkbox").prop("checked",true);
				}else{
					$("input[name='userId']:checkbox").prop("checked",false);
				}
			});

			toChangeUserDep();
		});
		
		var shanChuBM=function(id){
			$.ajax({
				url:"${ctx}/system/shanChuBM.do?id="+id,
				type:"POST",
				success:function(data){
					if(data.result=="hasChild")
						alert("此部门有下级部门，不能删除！");
					else if(data.result=="hasUser")
						alert("此部门下有用户，不能删除!");
					else{
						alert("删除成功");
						window.location.reload();
					}
				}
			});
		}


		
		var lockUser=function(id){
			$.ajax({
				url:"${ctx}/system/switchUserCK.do?id="+id,
				type:"POST",
				success:function(data){
					if(data.result=="none"){
						$.ajax({
							url:"${ctx}/system/switchUser.do?id="+id,
							type:"POST",
							success:function(data){
								alert("锁定成功！");
								window.location.reload();
							}
						});
					}else if(data.result=="inUse"){
						if(window.confirm("该用户有物资正在使用，真的要锁定该用户吗？")){
							$.ajax({
								url:"${ctx}/system/switchUser.do?id="+id,
								type:"POST",
								success:function(data){
									alert("锁定成功！");
									window.location.reload();
								}
							});
						}
					}else if(data.result=="has"){
						if(window.confirm("该用户有流程在办，真的要锁定该用户吗？")){
							$.ajax({
								url:"${ctx}/system/switchUser.do?id="+id,
								type:"POST",
								success:function(data){
									alert("锁定成功！");
									window.location.reload();
								}
							});
						}
					}
				}
			});
		}
		
		var unlockUser=function(id){
			$.ajax({
				url:"${ctx}/system/switchUser.do?id="+id,
				type:"POST",
				success:function(data){
					alert("解锁成功！");
					window.location.reload();
				}
			});
		}

        function deleteUser(userId) {
		    if (confirm('确定删除吗？')) {
                $.ajax({
                    type:"POST",
                    data: {userId: userId},
                    url:"${ctx }/system/user/delete.do",
                    dataType:"json",
                    success:function(result){
                        if(result.success){
                            getDepUsers(selectID);
                            alert("删除成功");
                        } else {
                            alert(result.message);
                        }
                    }
                });
			}
        }
        
        function toChangeUserDep() {
			$('#toChangeUserDep').bind('click',function(){
				var data = $("#userForm").parseForm();
				if (!data.userId || data.userId == '') {
					alert('请选择用户');
					return;
				}
				window.open("${ctx}/system/toChangeUserDep.do?userIds=" + data.userId, "blank");
			});
		}

	</script>
	<!-- END JAVASCRIPTS -->
</body>
<!-- END BODY -->
</html>